<?php
namespace Admin\Controller;
class LinkmenuCateController extends CommonController{
    public function _initialize(){
        parent::_initialize();
        $this->_mod = D('LinkmenuCate');
    }

    public function _before_index() {
        $big_menu = array(
            'title' => '增加分类',
            'iframe' => U('LinkmenuCate/add'),
            'id' => 'add',
            'width' => '400',
            'height' => '90'
        );
        $this->_ajaxedit = 1;
        $this->_data = array(
            'addchildmenu'    => array(
                'c' => 'Linkmenu',
                'a' => 'add',
                'str'    => '<a data-height="400" data-width="500" data-id="add" data-uri="%s" data-title="添加 - 添加子菜单" class="J_showdialog" href="javascript:;">添加子菜单</a>',
                'param' => array('id'=>'@id@'),
                'isajax' => 1,
                'replace'=> ''
            ),
            'menulist'    => array(
                'c' => 'Linkmenu',
                'a' => 'index',
                'str'    => '<a href="%s">子菜单管理</a>',
                'param' => array('id'=>'@id@'),
                'isajax' => 0,
                'replace'=> ''
            ),
        );
        $this->assign('big_menu', $big_menu);
    }
    public function delete(){
        $pk = $this->_mod->getPk();
        $pk =I($pk);

        $ids = trim($pk, ',');
        if ($ids) {
            if (false !== $this->_mod->delete($ids)) {
                IS_AJAX && $this->ajaxReturn(1, L('operation_success'));
                $this->success(L('operation_success'));
            } else {
                IS_AJAX && $this->ajaxReturn(0, L('operation_failure'));
                $this->error(L('operation_failure'));
            }
        } else {
            IS_AJAX && $this->ajaxReturn(0, L('illegal_parameters'));
            $this->error(L('illegal_parameters'));
        }
    }
    public function ajax_check_name() {
        $name = I('name');
        $id = I('id', 0,'intval');
        if ($this->name_exists($name,$id)) {
            $this->ajaxReturn(0, L('flink_cate_already_exists'));
        } else {
            $this->ajaxReturn(1);
        }
    }

    public function name_exists($name,$id=0){
        $pk = $this->_mod->getPk();
        $where = "name='" . $name . "'  AND ". $pk ."<>'" . $id . "'";
        $result=$this->_mod->where($where)->count($pk);
        if($result){
            return 1;
        }else{
            return 0;
        }
    }
}